************************************************************************************************
* Title: Graphical display of number of studies planning to recruit participants in each country
* Manuscript Ref : Figure 3a of the manuscript
* Version: 14.2
* Required packages: catplot 
************************************************************************************************

version 14.2

*Install required packages if needed
foreach package in catplot {
cap which `package'
if _rc {
ssc install `package'
}
}

*Select your preferred scheme file here - the scheme used is available as scheme-iddocovid.scheme
local local_scheme = "iddocovid"

*Change to folder where the dataset is stored
cd "filepath"
use 2020-05-07_iddo_lsr_covid-19_sr.dta, clear

*Change to folder where you want results saved
cd "filepath"

*Only keep one instance of each country/st_reg_id combination
keep if pickone_ctry_study_record==1
bysort st_ctry_name: gen within_ctry_num = _n

*Generate a wb_incomegroup that's ordered from low to high
gen wb_incomegroup_cat = .
replace wb_incomegroup_cat=1 if wb_incomegroup=="Low income"
replace wb_incomegroup_cat=2 if wb_incomegroup=="Lower middle income"
replace wb_incomegroup_cat=3 if wb_incomegroup=="Upper middle income"
replace wb_incomegroup_cat=4 if wb_incomegroup=="High income"
label define wb_incomegroup_cat_lbl 1 "Low income" 2 "Lower middle income" 3 "Upper middle income" 4 "High income"
label values wb_incomegroup_cat wb_incomegroup_cat_lbl

*Encodes st_ctry_name in wb_incomegroup_cat then frequency order
bysort st_ctry_name: egen ctry_freq = count(1) /* Count of country */
gsort -wb_incomegroup_cat -ctry_freq
gen temp_ranking = _n
bysort st_ctry_name: egen temp_group_ranking = max(temp_ranking)
egen st_ctry_name_grp2 = group(temp_group_ranking st_ctry_name)
drop temp_ranking temp_group_ranking
labmask st_ctry_name_grp2, values(st_ctry_name) decode
label variable st_ctry_name_grp "Country"

*Generate Graph
catplot wb_incomegroup_cat st_ctry_name_grp2, bargap(-100) scheme(`local_scheme') ytitle(Number studies) ///
var1opts(label(labsize(*0.5))) blabel(bar, position(outside) format(%3.0f)) asyvars ///
title("{bf: A}", size(large) position(10) color(black) ) ///
var2opts(label(labsize(vsmall)))
foreach file_format in pdf emf tif {
graph export Figure3A.`file_format', replace
}

*Note Break in x-axis on Figure 3A presented on final manuscript is created post-exporting
*Digital manipualtion after export was done using GimP software (https://www.gimp.org/)
